#include "work_context.h"
#include "download_channel.h"

// WorkContext
WorkContext::WorkContext() {
}

WorkContext::~WorkContext() {
}

// IWorkProvider
bool WorkContext::Init() {
	DCHECK(CalledOnValidThread());
	download_channel_.reset(new DownloadChannel);

	// create io thread
	thread_.reset(new WorkThread("IPCThread", this));
	base::Thread::Options options;
	options.message_loop_type = MessageLoop::TYPE_IO;
	return thread_->StartWithOptions(options);
}

void WorkContext::Cleanup() {
	DCHECK(CalledOnValidThread());
	thread_->Stop();
}

IDownloadProxy* WorkContext::GetDownload() const {
	return download_channel_.get();
}

// WorkThread::Delegate
void WorkContext::OnInit(WorkThread* thr) {
	download_channel_->Create(thread_->message_loop());
}

void WorkContext::OnCleanup(WorkThread* thr) {
	download_channel_->Destroy();
}
